/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.colegiatura.bc;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.cip.esymons.colegiatura.be.GuiaRemision;

/**
 *
 * @author ADMINISTRADOR
 */
@Stateless
public class GuiaRemisionFacade implements GuiaRemisionFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(GuiaRemision guiaRemision) {
        em.persist(guiaRemision);
    }

    public void edit(GuiaRemision guiaRemision) {
        em.merge(guiaRemision);
    }

    public void remove(GuiaRemision guiaRemision) {
        em.remove(em.merge(guiaRemision));
    }

    public GuiaRemision find(Object id) {
        return em.find(GuiaRemision.class, id);
    }

    public List<GuiaRemision> findAll() {
        return em.createQuery("select object(o) from GuiaRemision as o").getResultList();
    }

    public Long buscaUltimo(){

        try{
        return  (Long) em.createQuery("select Max(gr.greNumero) from GuiaRemision as gr").getSingleResult();
        }catch(Exception ex){
            return null;
        }
    }

    public GuiaRemision buscaGuia(Integer guia){
        try{
        return (GuiaRemision) em.createQuery("select object(o) from GuiaRemision as o where o.greCodigo=?1").setParameter(1, guia).getSingleResult();
        }catch(Exception ex){
            return null;
        }
    }

    public GuiaRemision buscarNroGuia(Long numero){
        try{
        return (GuiaRemision)em.createQuery("select object(o) from GuiaRemision as o where o.greNumero=?1").setParameter(1, numero).getSingleResult();
        }catch(Exception ex){
            return null;
        }
    }

}
